package com.cl.service;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import com.cl.activity.EnterAppPasswordActivity;
import com.cl.db.dao.AppLockDao;

import java.util.List;

public class WatchDogService extends Service {

	private static final String TAG = "WatchDogService";
	private ScreenOffReceiver offReceiver;
	private DataChangedReceiver dataChangedReceiver;
	private InnerReceiver innerReceiver;
	private boolean flag;
	private ActivityManager am;
	private AppLockDao dao;
	private Intent intent;
	private String tempStopProtectackName;
	private List<String> protectPackNames;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Log.i(TAG, "看门狗服务已经开启了");
		
		offReceiver = new ScreenOffReceiver();
		dataChangedReceiver = new DataChangedReceiver();
		innerReceiver = new InnerReceiver();
		registerReceiver(offReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
		registerReceiver(innerReceiver, new IntentFilter("com.itheima.safe.stoptem"));
		registerReceiver(dataChangedReceiver, new IntentFilter("com.itheima.safe.datachanged"));
		
		flag = true;
		am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		intent = new Intent(getApplicationContext(), EnterAppPasswordActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		dao = new AppLockDao(this);
		protectPackNames = dao.findAll();
		new Thread(){
			public void run() {
				while(flag) {
					List<RunningTaskInfo> taskInfos = am.getRunningTasks(1);
					String packName = taskInfos.get(0).topActivity.getPackageName();
					System.out.println("当前用户操作的应用程序:" + packName);
					if(protectPackNames.contains(packName)) {
						if(packName.equals(tempStopProtectackName)){
							
						}else {
							intent.putExtra("packName", packName);
							startActivity(intent);
						}
					}
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
		}.start();
		super.onCreate();
		
	}
	@Override
	public void onDestroy() {
		unregisterReceiver(offReceiver);
		unregisterReceiver(innerReceiver);
		unregisterReceiver(dataChangedReceiver);
		dataChangedReceiver = null;
		offReceiver = null;
		innerReceiver = null;
		flag = false;
		super.onDestroy();
	}
	private class ScreenOffReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(TAG, "屏幕锁屏了。。。");
			tempStopProtectackName = null;
			
		}
	}
	private class DataChangedReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(TAG, "屏幕解锁了。。。");
			protectPackNames = dao.findAll();
		}
	}
	private class InnerReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(TAG, "密码正确，进入加锁的程序");
			tempStopProtectackName = intent.getStringExtra("packName");		
		}
	}
}
